import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
/**
 * Demo MIDlet creates, runs and displays DemoGameCanvas.
 *
 * @author  0812506-0812517
 * @version 2.0.1
 */
public class GameMidlet extends MIDlet {
    private Display display;
    
    private Image splashLogo; 
    private boolean isSplash = true; 

    private MainMenuScreen mainMenuScreen; 
    private SettingsScreen settingsScreen; 
    private HighScoreScreen highScoreScreen; 
    private HelpScreen helpScreen; 
    private AboutScreen aboutScreen; 
    private GameScreen gameScreen;
    
    //For error handling
    private Alert alert;
    private static final boolean CONST_DEBUG = true;
    
    //Saving
    private static HighScore score; 
    private static Settings settings; 

    public void startApp() {
        try {
                Player player = Manager.createPlayer(getClass().getResourceAsStream("/start.wav"),"audio/x-wav");
                player.start();
            }catch(Exception e){
                e.printStackTrace();
            }
        
        try {
            settings = new Settings(); 
            settings.loadSettings(); 

            score = new HighScore(); 
            score.loadScores(); 

            display = Display.getDisplay(this); 
            mainMenuScreen = new MainMenuScreen(this); 
            settingsScreen = new SettingsScreen(this, settings); 
            highScoreScreen = new HighScoreScreen(this, score); 
            helpScreen = new HelpScreen(this);
            aboutScreen = new AboutScreen(this);


            if(isSplash) {
                isSplash = false;
                splashLogo = Image.createImage("/splash.png"); 
                new SplashScreen(display, mainMenuScreen, splashLogo,3000);
            }
            else {
                mainMenuScreenShow(null);
            }
        } catch(Exception ex) {
            showErrorMsg(null);
        }
    }
    
    protected void showErrorMsg(String alertMsg) {
        if (alertMsg == null || CONST_DEBUG == false) {
            alertMsg = "Error Starting Mushroom Game, may or may not function correctly.  Please contact support.";
        }	
        alert = new Alert("Mushroom Game ERROR",alertMsg,null,null);
        alert.setTimeout(Alert.FOREVER);
        alert.setType(AlertType.ERROR);                       
        this.mainMenuScreenShow(alert);    	
  }
    
    public Display getDisplay() {
        return display;
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        System.gc();
        notifyDestroyed();
    }
    
    private Image createImage(String filename) {
        Image image = null;
        try {
          image = Image.createImage(filename);
        } catch (Exception e) {
        }
        return image;
    }
    
    public void mainMenuScreenShow(Alert alert) {
        if (alert==null)        
            display.setCurrent(mainMenuScreen);
        else
            display.setCurrent(alert,mainMenuScreen); 
    }

    protected void settingsScreenShow() { 
        try { 
            settingsScreen.init(); 
            display.setCurrent(settingsScreen); 
        } catch (Exception e) {
            showErrorMsg(null); 
        }
    }
    protected void highScoreScreenShow() { 
        try { 
            highScoreScreen.init(); 
            display.setCurrent(highScoreScreen); 
        } catch (Exception e) {
            showErrorMsg(null); 
        }
    }
    
    protected void gameScreenShow() { 
        try {
            gameScreen = null; 
            gameScreen = new GameScreen(this,settings,score, Level.Lake);       
            gameScreen.start(); 
            display.setCurrent(gameScreen); 
        } catch (Exception e) { 
            System.out.println(e); 
            showErrorMsg(null); 
        }
    }

    public void helpScreenShow() {
        display.setCurrent(helpScreen);
    }

    public void aboutScreenShow() {
        display.setCurrent(aboutScreen);
    }

    public void mainMenuScreenQuit() {
        destroyApp(true);
    }

    void loadGameScreenShow() {
        try {
            gameScreen = null; 
            gameScreen = new GameScreen(this,settings,score, -1);       
            gameScreen.start(); 
            display.setCurrent(gameScreen); 
        } catch (Exception e) { 
            System.out.println(e); 
            showErrorMsg(null); 
        }
    }
}
